package org.cakejs2;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.app.ProgressDialog;
import android.content.Context;
import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import org.cakejs2.R;

public class MainActivity extends Activity {
  private WebView webView;

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.show_web_view);
    webView = (WebView) findViewById(R.id.webView1);

    //startWebView("http://...");
    //webView.loadUrl("file:///android_asset/error.html");
    startWebView("file:///android_asset/index.html");
  }

  private void startWebView(String url) {
    webView.getSettings().setJavaScriptEnabled(true);

    webView.getSettings().setDomStorageEnabled(true);
    webView.getSettings().setDatabaseEnabled(true);

    webView.getSettings().setDatabasePath("/data/data/" + 
              webView.getContext().getPackageName() + "/databases/");
//    webView.getSettings().setAllowUniversalAccessFromFileURLs(true);

//    webView.getSettings().setMixedContentMode(WebView.MIXED_CONTENT_ALWAYS_ALLOW);
//    webView.getSettings().setDomStorageEnabled(true);
//    webView.getSettings().setAllowContentAccess(true);
//    webView.getSettings().setAllowFileAccess(true);
//    webView.getSettings().setAllowFileAccessFromFileURLs(true);
//    webView.getSettings().setAllowUniversalAccessFromFileURLs(true);    


    /*
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.getSettings().setUseWideViewPort(true);
    webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    webView.setScrollbarFadingEnabled(false);
    webView.getSettings().setBuiltInZoomControls(true);

    //Additional Webview Properties 
    //
    webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    webView.getSettings().setDatabaseEnabled(true);
    webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
    webView.getSettings().setAppCacheEnabled(true);
    webView.getSettings().setLayoutAlgorithm(webView.getSettings().getLayoutAlgorithm().NORMAL);
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.getSettings().setUseWideViewPort(false);
    webView.setSoundEffectsEnabled(true);
    webView.setHorizontalFadingEdgeEnabled(false);
    webView.setKeepScreenOn(true);
    webView.setScrollbarFadingEnabled(true);
    webView.setVerticalFadingEdgeEnabled(false);
*/

    /*
       String summary = "<html><body>You scored <b>192</b> points.</body></html>";
       webview.loadData(summary, "text/html", null);
       */
    //Load url in webview
    webView.loadUrl(url);
  }

  @Override
  // Detect when the back button is pressed
  public void onBackPressed() {
    if(webView.canGoBack()) {
      webView.goBack();
    } else {
      // Let the system handle the back button
      super.onBackPressed();
    }
  }
}

